﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using GMap.NET.Projections;
using GMap.NET;
using System.Threading;

namespace _0512301_0512430.Main
{
    public partial class ucMaps : UserControl
    {
        MapSlate _currentState = new MapSlate();
        PictureBox[,] _slates = new PictureBox[3, 3];

        private int X1 = 0;
        private int Y1 = 0;

        private int X2 = 0;
        private int Y2 = 0;

        public static int disX = 0;
        public static int disY = 0;

        Thread thLoad;
        public ucMaps()
        {
            InitializeComponent();
            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    _slates[i, j] = new PictureBox();
                    _slates[i, j].Size = new System.Drawing.Size(214, 214);
                    _slates[i, j].Location = new System.Drawing.Point(i * 214, j * 214);
                    //_slates[i, j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    this.Controls.Add(_slates[i, j]);

                    // add action
                    this._slates[i,j].MouseLeave += new System.EventHandler(this.pbMap_MouseLeave);
                    this._slates[i,j].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pbMap_MouseDown);
                    this._slates[i,j].MouseHover += new System.EventHandler(this.pbMap_MouseHover);
                    this._slates[i,j].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pbMap_MouseUp);

                    this._slates[i, j].MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.pbMap_ZoomIn);
                }
        }
        
        
        public void findMaps()
        {
            LatLng latlng = this.GetLatLng(Main.frmMain.strFind);

            _currentState.Latitude = latlng.Latitude;
            _currentState.Longitude = latlng.Longitude;

            this.threadLoadImage();
        }

        private void addMapFound(Image imageFound)
        {
            //this.pbMap.Image = imageFound;
        }
        // Load Image
        private void loadImage()
        {
            // Nếu không thể kết nối tới Internet
            if (!Internet.Internet.IsConnectedToInternet())
            {
                return;
            }
            MercatorProjection mp = new MercatorProjection();
            GMap.NET.Point cp = mp.FromLatLngToPixel(_currentState.Latitude, _currentState.Longitude, _currentState.Zoom);

            for (int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    GMap.NET.Point np = cp;
                    np.Offset((-1 + i) * 214, (j - 1) * 214);
                    GMap.NET.PointLatLng pll = mp.FromPixelToLatLng(np, _currentState.Zoom);
                    _slates[i, j].Image = MapSlate.GetMapImage(pll.Lat, pll.Lng, _currentState.MapType, _currentState.Zoom, 214);
                }
        }
        public void threadLoadImage()
        {
            ucMaps ucmap = new ucMaps();
            thLoad = new Thread(new ThreadStart(this.loadImage));
            
            try
            {
                thLoad.Start();
            }
            catch (ThreadStateException)
            {
                MessageBox.Show("Can not connect to Google");
                this.threadStopLoadImage();
                return;
            }

        }
        public void threadStopLoadImage()
        {
            thLoad.Abort();
        }
        
        private void ucMaps_Load(object sender, EventArgs e)
        {
            this.threadLoadImage();
        }
        public LatLng GetLatLng(String center)
        {
            return _currentState.GetLatLng(center);
        }
        public void ZoomIn()
        {
            _currentState.Zoom++;
            this.threadLoadImage();

        }
        public void ZoomOut()
        {
            _currentState.Zoom--;
            this.threadLoadImage();
        }

        public void MoveLeft()
        {
            _currentState.Longitude -= 2;
            this.threadLoadImage();
        }

        public void MoveRight()
        {
            _currentState.Longitude += 2;
            this.threadLoadImage();
        }
        public void MoveUp()
        {
            _currentState.Latitude += 2;
            this.threadLoadImage();
        }
        public void MoveDown()
        {
            _currentState.Latitude -= 2;
            this.threadLoadImage();
        }
        public void Drag()
        {
            MercatorProjection mp = new MercatorProjection();
                    
            GMap.NET.PointLatLng pll1 = mp.FromPixelToLatLng(X1, Y1, _currentState.Zoom);        
            GMap.NET.PointLatLng pll2 = mp.FromPixelToLatLng(X2, Y2, _currentState.Zoom);        

            _currentState.Latitude += pll1.Lat - pll2.Lat;
            _currentState.Longitude += pll1.Lng - pll2.Lng;
        }

        private void pbMap_Click(object sender, EventArgs e)
        {
            Main.frmMain.strFind = "ha+noi";
            this.ucMaps_Load(sender, e);

        }

        private void pbMap_MouseHover(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
        }

        private void pbMap_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Arrow;
        }
        private void pbMap_ZoomIn(object sender, EventArgs e)
        {
            this.ZoomIn();
            this.threadLoadImage();
        }

        private void pbMap_MouseDown(object sender, MouseEventArgs e)
        {
            this.Cursor = new Cursor("../../Cursor/hmove.cur");
            X1 = Cursor.Position.X;
            Y1 = Cursor.Position.Y;
        }

        private void pbMap_MouseUp(object sender, MouseEventArgs e)
        {
            X2 = Cursor.Position.X;
            Y2 = Cursor.Position.Y;

            disX = X2 - X1;
            disY = Y2 - Y1;

            this.Cursor = Cursors.AppStarting;
            this.Drag();
            this.threadLoadImage();
            this.Cursor = Cursors.Hand;
        }

    }
}
